<?php
    require_once('qcubed.inc.php');
        class LoginForm extends QForm {
            protected $txtUserName;
            protected $txtPassword;
            protected $btnLogin;
            protected $lblResult;
            protected $authSys;
                // Define all the QContrtol objects for our Calculator
        protected function Form_Create() {
            $this->authSys = new Perm();
            if ( $this->authSys->IsAuthenticated(false) == true ){
                //Already auth
                $this->Redirect2Index();
                
            }

            $this->txtUserName = new QTextBox($this);
            $this->txtUserName->Required = TRUE;
            

            $this->txtPassword = new QTextBox($this);
            $this->txtPassword->Required = TRUE;
            $this->txtPassword->TextMode = QTextMode::Password;

            $this->lblResult = new QLabel($this);
            $this->lblResult->HtmlEntities = false;

            $this->btnLogin = new QButton($this);
            $this->btnLogin->Text = 'Login';
            $this->btnLogin->CausesValidation = true;
            
            $this->btnLogin->AddAction(new QClickEvent(), new QServerAction('btnLogin_Click'));
            

        }

                protected function Form_Validate() {
                // By default, we report that Custom Validations passed
                $blnToReturn = true;

                $blnFocused = false;
                foreach ($this->GetErrorControls() as $objControl) {
                        // Set Focus to the top-most invalid control
                        if (!$blnFocused) {
                                $objControl->Focus();
                                $blnFocused = true;
                        }
                        $objControl->Blink();
                }

                return $blnToReturn;
        }



        // Perform the necessary operations on the operands, and output the value to the lblResult
        protected function btnLogin_Click($strFormId, $strControlId, $strParameter) {
            if ($this->authSys->Authenticate($this->txtUserName->Text, $this->txtPassword->Text) == false)
                 $this->lblResult->Text = '<b>Wrong Username or password</b>';
            else
                $this->lblResult->Text = '<b>Thank you for logging in</b>  <a href="'. __APPS_WWW__ . '">Click Me</a>';
        }
        protected function Redirect2Index(){
            QApplication::Redirect(__APPS_WWW__);
        }
    }
    
    // And now run our defined form
    LoginForm::Run('LoginForm','login.tpl.php');
?>
        
        